package com.example.jack.community.persenter.login;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.example.jack.community.data.loginbean.RegisterStatus;
import com.example.jack.community.data.loginbean.RegisterVer;
import com.example.jack.community.model.login.Forget_bView;
import com.example.jack.community.view.login.util.Md5;
import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import okhttp3.Call;

/**
 * Created by TulinTonJie on 2017/11/8.
 */

public class ForgetPasswordPresenter implements Forget_bPresenterImp {

    private static final String TAG = "TonJie";
    //找回密码接口
    private Forget_bView forget_bViewt;

    private Context context;

    public ForgetPasswordPresenter(Context context) {
        this.context = context;
    }

    private static String registerVerstring = null;


    @Override
    public void forgetpassword(String password_a, String password_b, String phonenum) {
        Log.d(TAG, "forgetpassword: " + password_a + "" + password_b + "" + phonenum);
        if (!TextUtils.isEmpty(password_a)         //请输入密码不为空
                && !TextUtils.isEmpty(password_b) // 请确认密码部位空
                && !TextUtils.isEmpty(phonenum)  //请输入你的手机号
                ) {
            if (password_a.equals(password_b)) {
                forgetpasswords(password_a, phonenum);
            } else {
                forget_bViewt.showToast("很抱歉，您两次输入的账户密码不一样，请重新输入");
            }
        } else

        {
            forget_bViewt.showToast("请输入要使用的密码");
        }

    }

    private void forgetpasswords(final String password_a, final String phonenum) {

        String url;
        url = "http://192.168.1.11/change/";
        OkHttpUtils
                .get()
                .url(url)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Log.d(TAG, "onError: " + id);
                    }

                    @Override
                    public void onResponse(String response, int id) {

                        Gson registerVer = new Gson();
                        RegisterVer registermd5 = registerVer.fromJson(response, RegisterVer.class);
                        Md5 register_md = new Md5();
                        String string;
                        string = register_md.md5(registermd5.getData());
                        registerVerstring = string.substring(20, 30);
                        Log.d(TAG, "onResponse: 最终输入的手机号码" + phonenum);
                        Log.d(TAG, "onResponse: username:" + phonenum + "    newpassword:" + password_a + "  telephone:" + phonenum + "   passcode:"
                                + registerVerstring + "   ChangeDataType:2");
                        Md5 md5 = new Md5();
                        final String passwords = md5.md5(password_a);
                        OkHttpUtils
                                .post()
                                .url("http://192.168.1.11/change/")
                                .addParams("username", phonenum)
                                .addParams("newpassword", passwords)
                                .addParams("oldpassword", "0")
                                .addParams("telephone", phonenum)
                                .addParams("passcode", registerVerstring)
                                .addParams("ChangeDataType", "2")
                                .build()
                                .execute(new StringCallback() {
                                    @Override
                                    public void onError(Call call, Exception e, int id) {

                                    }

                                    @Override
                                    public void onResponse(String response, int id) {
                                        Log.d(TAG, "onResponse: 进行post请求" + response);
                                        Gson gson = new Gson();

                                        RegisterStatus shopInfo = gson.fromJson(response, RegisterStatus.class);

                                        shopInfo.getStatus();

                                        if (shopInfo.getStatus().equals("0")) {
                                            forget_bViewt.ForgetSuccess("Reset the success");
                                        }
                                        if (shopInfo.getStatus().equals("2")) {
                                            forget_bViewt.ForgetFail("Users don't exist");
                                        }
                                    }
                                });
                    }
                });
    }


    @Override
    public void attachView(Forget_bView v) {
        this.forget_bViewt = v;
    }

    @Override
    public void detachView() {
        forget_bViewt = null;
    }
}
